home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / mail / mh / contrib / queuemh / queuemh-scripts / packq < prev    next >
Encoding:
Text File  |  1992-11-08  |  2.2 KB  |  121 lines

  1. #!/usr/local/bin/perl
  2. #
  3. # This does a specific kinda sortm... sorts in reverse order
  4. # on a specific textfield and date secondarily.
  5. # It does NOT use the sortm command.  
  6. #
  7. # It should be executed with caution - the folder should
  8. # be quiescent, i.e. no res or stats in progress.
  9. #
  10.  
  11. $textfield="Priority";
  12. $HOME="$ENV{'HOME'}";
  13. $PWD="$ENV{'PWD'}";
  14.  
  15. ##
  16. # set the folder
  17. #
  18. $default="trouble";
  19. $folder=`folder -fast`; chop $folder;
  20.  
  21. ##
  22. #parse args
  23. #
  24. while ($#ARGV > -1)
  25. {
  26.     if ( $ARGV[0] =~ /^\+(\w*)/ )
  27.     {
  28.         $folder = $1;
  29.         shift;
  30.     }
  31.     elsif ( $ARGV[0] =~ /^-t/ )
  32.     {
  33.         $textfield = $ARGV[1];
  34.         shift; shift;
  35.     }
  36.     elsif ( $ARGV[0] =~ /^-v/ )
  37.     {
  38.         $verbose = "yes";
  39.         shift;
  40.     }
  41.     elsif ($ARGV[0] eq "-help" )
  42.     {
  43.         print "packq sorts a folder in reverse order on $textfield.\n";
  44.         print "Usage:\tpackq [+folder] [-verbose] [-textfield value]\n";
  45.         exit;
  46.     }
  47.     else
  48.     {
  49.         die "I Don't grok $ARGV[0]\n";
  50.     }
  51. }
  52. $folder = $default if ( "$folder" eq '' ) ;
  53.  
  54.  
  55. ##
  56. # set the path to the mh mail dir
  57. #
  58. $PROFILE="$HOME/.mh_profile";
  59. $MAILPATH="$HOME/Mail";
  60. open(PROFILE);
  61. while (<PROFILE>)
  62. {
  63.     $MAILPATH = "$HOME/$1" if ( /^Path: (\w*)/ ) && last;
  64. }
  65. close(PROFILE);
  66.  
  67. ##
  68. # get the list of message numbers
  69. # build assoc array keyed by msg number
  70. opendir(FOLDER,"$MAILPATH/$folder") || die "Gack, $MAILPATH/$folder: $!\n";
  71. @files = grep(/^\d/, readdir(FOLDER));
  72. closedir(FOLDER);
  73. chdir("$MAILPATH/$folder");
  74.  
  75. ##
  76. # move messages out of the way and get the textfield value
  77. # make textfield value the value in 
  78. print "getting $textfield\n" if ( $verbose eq "yes");
  79. foreach $msg (@files)
  80. {
  81.     rename("$msg","#$msg");
  82.     open(MSG,"#$msg") || print "Where did #$msg go?";
  83.     $value = "A1";
  84.     undef $date;
  85.     while (<MSG>)
  86.     {
  87.         $value = $1 if ( /^$textfield:\s*(\w*)/ );
  88.         if ( /Message-Id:\s*<(\d*)\./ )
  89.         {
  90.         $date = $1;
  91.         last;
  92.         }
  93.     }
  94.     $queue{$msg} = $value . $date;
  95.     close(MSG);
  96. }
  97.  
  98. ##
  99. #sort in reverse by value and put the messages back
  100. #
  101. print "Sorting...\n" if ( $verbose eq "yes" );
  102. @msglist = (sort by_value_descending keys(%queue));
  103. for ($i = 1; $i <= $#msglist + 1; $i++)
  104. {
  105.     print "$msglist[$i-1] becomes $i\n" if ( $verbose eq "yes");
  106.     rename("#$msglist[$i-1]","$i");
  107. }
  108.  
  109. ##
  110. # go back to where we started
  111. #
  112. chdir("$PWD");
  113.  
  114. ##
  115. # subroutines
  116. #
  117. sub by_value_descending
  118. {
  119.     $queue{$b} cmp $queue{$a};
  120. }
  121.